package com.itheima.filter; /**
 * @author MaQing
 * @create 2022-05-10 08:50
 */

import com.itheima.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebFilter(value = "/contact/*",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class ContactFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request1 = (HttpServletRequest) request;

        //判断
        HttpSession session = request1.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {

            //获取路径，请求转发之前的路径
            String uri = request1.getRequestURI();

            //获取请求标记，将标记拼接到请求路径中
            String method = request1.getParameter("method");

            session.setAttribute("oldUrl",uri+"?method="+method);


            //请求转发
            request1.getRequestDispatcher("/login.jsp").forward(request1,response);
            return;
        }

        chain.doFilter(request1, response);
    }
}
